package com.cnhis.cloudhealth.clinical.hosnurse.hosnewadvice.bo;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.hosnurse.hosnewadvice.dao.HosNewAdviceDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.ComVerUtils;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.MapUtils;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

import net.sf.json.JSONObject;

@Service
public class HosNewAdviceBo {
 @Resource
 private HosNewAdviceDao hosNewAdviceDao;
 
 @Autowired
 private GetNewId getNewId;
 private String vaf59r;

 public ModelVo getclinicEditOrderBeforeJudge(Map map) throws Exception {

     ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);

     // 如果是非门诊，则返回成功
     if (!"1".equals(map.get("ACF01").toString())) {
         return mo;
     }
     // 是否有限制条件
     if (getBlp02(map)) {
         return mo;
     }

     // 获取限定年龄
     Integer BLP04 = getBlp04(map);

     // 获取病人年龄
     Integer VAA10 = getVaa10(map);

     if ((VAA10 >= BLP04) && getVcj04Vcj05(map)) {
         mo.setResult(StaticKeys.OH_FALSE);
         mo.setResultMsg("病人超过" + BLP04 + "岁，必须要输入收缩压和舒张压.");
         return mo;
     }

     return mo;
 }

 /**
  * 入收缩压和舒张压 是否为空
  *
  * @param map
  * @return
  * @throws Exception
  */
 private boolean getVcj04Vcj05(Map map) throws Exception {
     return hosNewAdviceDao.getVcj04Vcj05(map)>0;
 }

 /**
  * 获取病人年龄
  *
  * @param map
  * @return
  * @throws Exception
  */
 private Integer getVaa10(Map map) throws Exception {
     Integer VAA10 = hosNewAdviceDao.getVaa10(map);
     VAA10 = VAA10 == null ? 0 : VAA10;
     return VAA10;
 }

 /**
  * 获取限定年龄
  *
  * @param map
  * @return
  * @throws Exception
  */
 private Integer getBlp04(Map map) throws Exception {
     Integer BLP04 = hosNewAdviceDao.getBlp04(map);
     BLP04 = BLP04 == null ? 0 : BLP04;
     return BLP04;
 }

 /**
  * 是否有限制条件
  *
  * @param map
  * @return
  * @throws Exception
  */
 private boolean getBlp02(Map map) throws Exception {
     return hosNewAdviceDao.getBlp02(map)==0;
 }

 /**
  * 临床药物评价 Clinic_Drug_Judge
  *
  * @param map
  * @return
  * @throws Exception
  */
 public ModelVo clinicDrugJudge(Map map) throws Exception {

     ModelVo mo = null;

     String lxml = map.get("lxml").toString();
     // 将lxml装换为json
     JSONObject job = JSONObject.fromObject(lxml);

     // 获取医嘱单信息
     List TmpCBM = JsonUtil.parserToList(job.getJSONArray("cbm1"));

     if (TmpCBM.size() == 0) {
         return new ModelVo(StaticKeys.OH_FALSE, "医嘱单信息为空");
     }
     // 获取医嘱信息
     List TmpVAF = getTmpVAF(job, TmpCBM);
     if (TmpVAF.size() == 0) {
         return new ModelVo(StaticKeys.OH_FALSE, "医嘱信息为空");
     }

     // 获取诊断信息
     List TmpVAO = getTmpVAO(job);

     // 获取病人诊疗申请单信息
     List TmpVBQ = getTmpVBQ(job);

     // 获取病人手术人员信息
     List TmpVAX = getTmpVAX(job);

     String ACF01 = map.get("ACF01").toString();

     String asign = map.get("asign").toString();

     // 修改执行科室ID
     TmpVAF = getTmpVAFByBck01b(TmpVAF, asign);

     // 获取参数
     Map params = getParams(map, ACF01);

     Map cdjMap = new HashMap<>();
     cdjMap.put("tmpCBM", TmpCBM);
     cdjMap.put("tmpVAF", TmpVAF);
     cdjMap.put("tmpVAO", TmpVAO);
     cdjMap.put("tmpVBQ", TmpVBQ);
     cdjMap.put("tmpVAX", TmpVAX);
     
     List<HashMap> list = new ArrayList<HashMap>();
     HashMap temp = new HashMap();
		list = hosNewAdviceDao.getAllBLPList(temp);
     for(HashMap hashMap : list)
     {
     	temp.put("txt", "select "+hashMap.get("BLP07"));
     	List ls = hosNewAdviceDao.execProc(temp);
     }
     
     

     // 手术医嘱判断
     mo = getJudgeSs(TmpVAF, TmpVAX, params, ACF01);
     if (mo != null) {
         return mo;
     }

     // 外配处方，需要整张处方药品执行性质都是外配处方，不能有别的执行性质，请检查.
     if ("1".equals(asign) && "1".equals(ACF01)) {
         mo = getJudNoTotal(cdjMap);
         if (mo != null) {
             return mo;
         }
     }

     // 离院带药，需要处方药品中本组药品执行性质都是离院带药，本组不能有别的执行性质，请检查.
     if ("1".equals(asign)) {
         mo = getJudLydy(cdjMap);
         if (mo != null) {
             return mo;
         }
     }

     // 西成药品医嘱行数
     int tcount = getJudgeXcyCt(TmpVAF);

     // 抗生素类药品用药目的判断
     mo = getJudgeKssYymd(tcount, params, cdjMap, asign);
     if (mo != null) {
         return mo;
     }

     // 判断处方诊断
     mo = getJudgeCfPd(tcount, params, TmpCBM, TmpVAO);
     if (mo != null) {
         return mo;
     }

     // 只判断西成药品处方
     if (tcount > 0) {
         TmpVAF = getJudgeOnlyXcy(cdjMap);
     }
     cdjMap.put("tmpVAF", TmpVAF);
     // 已超出限售金额;超出限售数量
     // 这里是要统计全院的的药品销售情况

     mo = getJudgeExceed(TmpVAF, params, tcount);

     return new ModelVo(StaticKeys.OH_SUCCESS);
 }


 private ModelVo getJudgeExceed(List tmpVAF, Map params, int tcount) throws Exception {
     if (tcount == 0) {
         return null;
     }

     String gpara69 = params.get("gpara69").toString();
     if (gpara69 == null || "0".equals(gpara69)) {
         return null;
     }

     // 西、成药的限额是否大于1
     boolean xcf = getJEXcf(tmpVAF);

     String adate = params.get("edate").toString().substring(0, 7) + "-01";
     params.put("adate", adate);

     Map map = new HashMap<>();
     map.put("tmpVAF", tmpVAF);
     map.put("adate", adate);
     map.putAll(params);
     ;

     String smsg = null;
     // 已超出限售金额
     if ("1".equals(gpara69) && xcf) {
         smsg = hosNewAdviceDao.getJudgeXsje(map);
     }
     // 超出限售数量
     if ("2".equals(gpara69) && xcf) {
         smsg = hosNewAdviceDao.getJudgeXssl(map);
     }

     if (smsg != null && (!"".equals(smsg))) {
         return new ModelVo(StaticKeys.OH_FALSE, smsg + "，不能再下达此药品.");
     }

     return null;
 }

 private boolean getJEXcf(List tmpVAF) {
     Map map;
     String bag46;
     String bda01;
     int l = tmpVAF.size();

     for (int i = 0; i < l; i++) {
         map = (Map) tmpVAF.get(i);
         bag46 = MapUtils.getString(map, "bag46");
         bda01 = MapUtils.getString(map, "bda01");
         if (bag46 != null && bda01 != null) {
             if (bag46.compareTo("1") > 0 && ("1".equals(bda01) || "2".equals(bda01))) {
                 return true;
             }
         }
     }

     return false;
 }

 private List getJudgeOnlyXcy(Map cdjMap) throws Exception {
     return hosNewAdviceDao.getJudgeOnlyXcy(cdjMap);
 }

 private ModelVo getJudgeCfPd(int tcount, Map params, List tmpCBM, List tmpVAO) {
     String mzpara153 = params.get("mzpara153").toString();
     if (tcount > 0 && "1".equals(mzpara153)) {

         Set set = new HashSet<>();
         Map map = null;
         String cbm06 = null;
         int tc = tmpCBM.size();
         // cbm06 性质: 0:不区分中、西药处方 1=西、成药, 2=中药, 3=手术记录, 4 = 成药,5=卫材, 9=其他
         for (int i = 0; i < tc; i++) {
             map = (Map) tmpCBM.get(i);
             cbm06 = MapUtils.getString(map, "cbm06");
             if (cbm06 != null && (!("1".equals(cbm06) || "2".equals(cbm06) || "4".equals(cbm06)))) {
                 return null;
             }
         }

         String cbmid = null;
         int tv = tmpVAO.size();
         for (int i = 0; i < tv; i++) {
             map = (Map) tmpVAO.get(i);
             cbmid = MapUtils.getString(map, "cbmid");
             if (cbmid != null) {
                 set.add(cbmid);
             }
         }
         if (set.size() == 0) {
             return null;
         }

         for (int i = 0; i < tc; i++) {
             map = (Map) tmpCBM.get(i);
             cbmid = MapUtils.getString(map, "cbmid");
             if (set.contains(cbmid)) {
                 cbm06 = MapUtils.getString(map, "cbm06");
                 if (cbm06 != null && (!("1".equals(cbm06) || "2".equals(cbm06) || "4".equals(cbm06)))) {
                     return new ModelVo(StaticKeys.OH_FALSE, "药品处方没有诊断，请填写处方诊断.");
                 }
             }
         }
     }

     return null;
 }

 @SuppressWarnings("unchecked")
 private ModelVo getJudgeKssYymd(int tcount, Map params, Map cdjMap, String asign) throws Exception {
     String para356_392 = params.get("para356_392").toString();
     cdjMap.put("asign", asign);
     if (tcount > 0 && "1".equals(para356_392)) {
         String vaf22 = hosNewAdviceDao.getJudgeKssYymd(cdjMap);
         if (vaf22 != null && (!"".equals(vaf22))) {
             return new ModelVo(StaticKeys.OH_FALSE, vaf22 + " 没有用药目的，请填写用药目的.");
         }
     }
     return null;
 }

 private int getJudgeXcyCt(List tmpVAF) {
     int l = 0;
     int tv = tmpVAF.size();
     Map map = null;
     String bda01 = null;
     for (int i = 0; i < tv; i++) {
         map = (Map) tmpVAF.get(i);
         bda01 = MapUtils.getString(map, "bda01");
         if ("1".equals(bda01) || "2".equals(bda01) || "3".equals(bda01)) {
             l++;
         }

     }
     return l;
 }

 private List getTmpVAX(JSONObject job) {
     List ls = new ArrayList();
     if (job.containsKey("vax1")) {
         ls = JsonUtil.parserToList(job.getJSONArray("vax1"));
         int ll = ls.size();
         Map map = null;
         for (int i = 0; i < ll; i++) {
             map = (Map) ls.get(i);
             map.put("vaf01", 0);
             map.put("vax01", 0);
             map.put("vat01", 0);
         }
     }
     return ls;
 }

 private List getTmpVBQ(JSONObject job) {
     List ls = new ArrayList();
     if (job.containsKey("vbq1")) {
         ls = JsonUtil.parserToList(job.getJSONArray("vbq1"));
         int ll = ls.size();
         Map map = null;
         for (int i = 0; i < ll; i++) {
             map = (Map) ls.get(i);
             map.put("vbqid", 0);
         }
     }
     return ls;
 }

 private List getTmpVAO(JSONObject job) {
     List ls = new ArrayList();
     if (job.containsKey("vao1")) {
         ls = JsonUtil.parserToList(job.getJSONArray("vao1"));
         int ll = ls.size();
         Map map = null;
         for (int i = 0; i < ll; i++) {
             map = (Map) ls.get(i);
             map.put("bak02", "");
         }
     }
     return ls;
 }

 private List getTmpVAF(JSONObject job, List tmpCBM) {
     int ll = tmpCBM.size();
     Set set = new HashSet<>();
     String cbmid = null;
     Map map = null;
     for (int i = 0; i < ll; i++) {
         map = (Map) tmpCBM.get(i);
         cbmid = MapUtils.getString(map, "cbmid");
         if (cbmid != null) {
             set.add(cbmid);
         }
     }

     List rs = new ArrayList<>();

     List ls = JsonUtil.parserToList(job.getJSONArray("vaf1"));
     ll = ls.size();
     String lsign = null;

     for (int i = 0; i < ll; i++) {
         map = (Map) ls.get(i);
         lsign = MapUtils.getString(map, "lsign");
         if (lsign != null && "0".equals(lsign)) {
             cbmid = MapUtils.getString(map, "cbmid");
             if (set.contains(cbmid)) {
                 map.put("bag46", 0d);
                 map.put("bbt07", 0);
                 map.put("bby23", 0d);
                 rs.add(map);
             }
         }
     }

     return rs;
 }

 private ModelVo getJudLydy(Map map) throws Exception {
     if (hosNewAdviceDao.getJudLydy(map) > 0) {
         return new ModelVo(StaticKeys.OH_FALSE, "离院带药，需要处方药品中本组药品执行性质都是离院带药，本组不能有别的执行性质，请检查.");
     }
     ;
     return null;
 }

 private ModelVo getJudNoTotal(Map map) throws Exception {
     if (hosNewAdviceDao.getJudNoTotal(map) > 0) {
         return new ModelVo(StaticKeys.OH_FALSE, "外配处方，需要整张处方药品执行性质都是外配处方，不能有别的执行性质，请检查.");
     }
     return null;
 }

 private ModelVo getJudgeSs(List tmpVAF, List tmpVAX, Map params, String ACF01) throws Exception {

     if (!"2".equals(ACF01)) {
         return null;
     }
     int tv = tmpVAF.size();

     Map map = null;
     String bda01 = null;
     int cnt = 0;
     for (int i = 0; i < tv; i++) {
         map = (Map) tmpVAF.get(i);
         bda01 = MapUtils.getString(map, "bda01");
         if (bda01 != null) {
             if ("S".equals(bda01) || "0".equals(bda01)) {
                 cnt++;
             }
         }
     }
     if (cnt == 0) {
         return null;
     }

     cnt = getJudgeSsMz(tmpVAF);
     if (cnt == 0) {
         return null;
     }

     String zyPara386 = params.get("zypara386").toString();
     Map mls = new HashMap();
     mls.put("tmpVAF", tmpVAF);
     mls.put("tmpVAX", tmpVAX);
     // 没有麻醉方式，请检查.
     if ("1".equals(zyPara386)) {
         String vaf22 = getJudgeMzfs(mls);
         if (vaf22 != null && !"".equals(vaf22)) {
             return new ModelVo(StaticKeys.OH_FALSE, vaf22 + "，没有麻醉方式，请检查.");
         }
         // 没有主刀医师，请检查.
         vaf22 = getJudgeNoZd(mls);
         if (vaf22 != null && !"".equals(vaf22)) {
             return new ModelVo(StaticKeys.OH_FALSE, vaf22 + "，没有主刀医师，请检查.");
         }
     }

     // 没有切口等级，请检查.
     cnt = getBlpBlp0230();
     if (cnt > 0) {
         String vaf22 = hosNewAdviceDao.getJudgeNoQkdj(mls);
         if (vaf22 != null && !"".equals(vaf22)) {
             return new ModelVo(StaticKeys.OH_FALSE, vaf22 + "，没有切口等级，请检查.");
         }
     }

     return null;
 }

 private int getBlpBlp0230() throws Exception {
     return hosNewAdviceDao.getBlpBlp0230();
 }

 private String getJudgeNoZd(Map mls) throws Exception {
     return hosNewAdviceDao.getJudgeNoZd(mls);
 }

 private String getJudgeMzfs(Map mls) throws Exception {
     return hosNewAdviceDao.getJudgeMzfs(mls);
 }

 private int getJudgeSsMz(List tmpVAF) throws Exception {
     return hosNewAdviceDao.getJudgeSsMz(tmpVAF);
 }

 private Map getParams(Map map, String ACF01) throws Exception {
     Short isybbr = 0;

     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
     Date date = new Date();

     Calendar cl = Calendar.getInstance();
     cl.setTime(date);
     cl.add(Calendar.MONTH, -2);// 减2个月

     Integer type = Integer.valueOf(map.get("type").toString());

     String edate = sdf.format(date);
     String vaj47 = sdf.format(cl.getTime());

     Map param = null;

     // 限制最大销售数量或金额判断
     if ("1".equals(ACF01)) {
         param = hosNewAdviceDao.getMaxSalePara(map);
         if (Integer.valueOf(param.get("cont").toString()) > 0) {
             isybbr = 1;
         }
     }
     if ("2".equals(ACF01)) {
         if (type.equals(1)) {
             param = hosNewAdviceDao.getMaxSale21Para(map);
         }
         if (type.equals(2)) {
             param = hosNewAdviceDao.getMaxSale22Para(map);
         }

         if (Integer.valueOf(param.get("cont").toString()) > 0) {
             isybbr = 1;
         }
     }

     param.put("edate", edate);
     param.put("vaj47", vaj47);
     param.put("isybbr", isybbr);

     return param;
 }

 private List getTmpVAFByBck01b(List tmpVAF, String asign) {
     if ("0".equals(asign)) {
         int ll = tmpVAF.size();
         Map map = null;
         String rcurrent = null;
         for (int i = 0; i < ll; i++) {
             map = (Map) tmpVAF.get(i);
             rcurrent = MapUtils.getString(map, "rcurrent");
             if (rcurrent != null && "1".equals(rcurrent)) {
                 map.put("bck01b", 0);
             }
         }
     }
     return tmpVAF;
 }

 /**
  * 新开医嘱 Clinic_VAF1_Insert
  *
  * @param map
  * @return
  * @throws Exception
  */
 public ModelVo insertClinicVAF1(Map map) throws Exception {
     ModelVo mo = null;

//     // 版本验证
//     String lver = map.get("lver").toString();
//
//     String ms = "0008.0000.0001.0143";

//     if (ComVerUtils.CompareVersion(lver, ms)) {
//         return new ModelVo(StaticKeys.OH_FALSE, "客户端版本" + lver + "校验失败，最低版本号(" + ms + "),请重启软件进行升级.");
//     }
     Integer lbce01 = Integer.valueOf(map.get("lbce01").toString());

     String lbce02 = map.get("lbce02").toString();
     String lbce03 = map.get("lbce03").toString();

     String lxml = map.get("lxml").toString();
     // 将lxml装换为json
     JSONObject job = JSONObject.fromObject(lxml);

     // 获取医嘱单信息
     List TmpCBM = getCviTmpCBM(job, lbce01, lbce03);
     if (TmpCBM.size() == 0) {
         return new ModelVo(StaticKeys.OH_FALSE, "提交文档错误，医嘱单信息为空");
     }
     // 获取医嘱内容信息
     List TmpVAF = getCviTmpVAF(job, TmpCBM);
     if (TmpVAF.size() == 0) {
         return new ModelVo(StaticKeys.OH_FALSE, "提交文档错误，医嘱信息为空");
     }

     // 提交文档错误，医嘱开始时间不能省略. 这个可在客户端完成
     mo = getCviNoKzsj(TmpVAF);
     if (mo != null) {
         return mo;
     }

     // 获取病人诊断信息
     List TmpVAO = getCviTmpVAO(job);

     // 获取病人诊疗申请单
     List TmpVBQ = getCviTmpVBQ(job);

     // 获取病人手术人员信息
     List TmpVAX = getCviTmpVAX(job);

     // 没有用药频次，请修改 可在客户端完成
     mo = getCviNoyypc(TmpVAF);
     if (mo != null) {
         return mo;
     }
     // 获取参数
     Map para = getCviPara();

     // 修改给药科室ID信息
     TmpVAF = getCviUpGyks(TmpVAF);

     // 医嘱中执行科室不正确，请检查修改！' 可在客户端完成
     // 给药途径对应附加执行科室不正确，请检查修改！ 可在客户端完成
     // 医嘱中对应附加执行科室不正确，请检查修改！ 可在客户端完成
     // 医嘱中对应开嘱科室不正确，请检查修改！可在客户端完成
     // 医嘱中对应长嘱终止时间不正确，请检查修改！

     // 修改医嘱单 中的病人姓名
     TmpCBM = getCviBrxmTmpCBM(TmpCBM);

     // 获取医嘱单ID，并生成医嘱单信息，多个以逗号分隔 返回值
     String rCBM = getCviRcbm(TmpCBM);

     // 组装医嘱内容信息
     TmpVAF = getCviZz(TmpVAF, TmpCBM);

     // 更新病人诊疗申请单ID
     TmpVBQ = getUpdateTmpVBQ(TmpVBQ, TmpCBM);

     //更新病人诊断ID
     TmpVAO = getCviUpTmpVAO(TmpVAO, TmpCBM);

     //更新病人手术人员信息，获取病人医嘱附件
     List tmpVBF = getCviTmpVBF(TmpVAX, TmpVAF);

     //开始事务
     //先删除修改的医嘱单

     Integer lmode = Integer.valueOf(map.get("lmode").toString());//0:新增 1：修改 2=复诊预约计划
     Long lcbm01a = Long.valueOf(map.get("lcbm01a").toString());//修改医嘱单的id

     if (lmode.equals(1) && (lcbm01a > 0)) {
         mo = delclinicAdviceCBM01(map);

         if (StaticKeys.OH_FALSE.equals(mo.getResult())) {
             return mo;
         }
     }


     Map inMap = new HashMap<>();
     inMap.put("ldate", para.get("ldate"));
     inMap.put("lbce01", lbce01);
     inMap.put("lbce03", lbce03);
     inMap.put("tmpCBM", TmpCBM);

     //先增加医嘱单
     insertCviCBM1(inMap);

     //插入病人申请单信息
     insertCviVbq1(TmpVBQ, para);

     para.put("lbce01", lbce01);
     para.put("lbce03", lbce03);

     para.put("tmpVAF", TmpVAF);
     //插入医嘱内容信息
     insertCviVaf1(para);

     //插入病人诊断信息
     if (TmpVAO.size() > 0) {
         para.put("tmpVAO", TmpVAO);
         insertCviVao1(para);
     }

     //插入历史记录
     insertCviVbg1(para, TmpVAF);

     //更新私人诊疗项目
     insertCviBdj1(para);

     //更新病人手术人员信息
     insertCviVax(TmpVAX, tmpVBF);

     /* 挂号附表 */

     if (!lmode.equals(2)) {

         updateCviVcj1(TmpCBM, para,lbce01,lbce02,lbce03);
     }

     List ls = new ArrayList<>();
     ls.add(new HashMap<>().put("rCBM",rCBM));

     mo = new ModelVo(StaticKeys.OH_SUCCESS,"");
     mo.setList(ls);

     return mo;
 }

 private void updateCviVcj1(List tmpCBM, Map para, Integer lbce01, String lbce02, String lbce03) throws Exception {
     //医嘱插入目前只针对一个病人，所以取一个病人的挂号ID即可
     //if exists(select * from #TmpCBM a where not exists(select * from VCJ1 b where b.VAA07 = a.VAA07))
     Long vaa07 =null;
     String vaa07s;
     Map map;
     int tc = tmpCBM.size();
     for (int i = 0; i < tc; i++) {
         map = (Map) tmpCBM.get(i);
         vaa07s = MapUtils.getString(map, "vaa07");
         if (vaa07s != null) {
             vaa07 = Long.valueOf(vaa07s);
             break;
         }
     }

     //查看病人挂号附表中是否有该病人

     map = new HashMap();
     map.put("vaa07",vaa07);
     map.put("ldate",para.get("ldate"));
     int ct = hosNewAdviceDao.getCviHasVaa07(map);
     if(ct>0){
         //更新
         hosNewAdviceDao.updateCviVcj1(map);
     }
     else{
         //插入
         hosNewAdviceDao.insertCviVcj1(map);
     }

     String mzpara370 = MapUtils.getString(para,"mzpara370");
     if(mzpara370!=null&&"1".equals(mzpara370)){
         map.put("lbce01",lbce01);
         map.put("lbce02",lbce02);
         map.put("lbce03",lbce03);
         hosNewAdviceDao.updateCviFromMzpara370(map);
     }
 }

 private void insertCviVax(List tmpVAX, List tmpVBF) throws Exception {
     int tvx = tmpVAX.size();
     List ls = new ArrayList();
     Map map;
     String ace01;
     Long vaf01;
     for (int i = 0; i < tvx; i++) {
         map = (Map) tmpVAX.get(i);
         ace01 = MapUtils.getString(map, "ace01");
         vaf01 = Long.valueOf(map.get("vaf01").toString());
         if (ace01 != null && (!"--".equals(ace01)) && (vaf01 > 0)) {
             ls.add(map);
         }
     }

     if (ls.size() > 0) {
         hosNewAdviceDao.insertCviVax(ls);
     }

     int tvf = tmpVBF.size();
     if (tvf > 0) {
         hosNewAdviceDao.insertCviVaf(ls);
     }
 }

 /**
  * 更新私人诊疗项目
  *
  * @param para
  */
 private void insertCviBdj1(Map para) throws Exception {

     List kbmBDJ = hosNewAdviceDao.getCviKbmBDJ(para);
     para.put("kbmBDJ", kbmBDJ);

     //更新私人诊疗项目
     hosNewAdviceDao.updateCviBdj1(para);

     //获取需要插入的私人诊疗项目

     List inBdj = hosNewAdviceDao.getCviInDbj(para);

     int ib = inBdj.size();
     if(ib>0){
         List<Long> ls = getNewId.nextId(ib);
         String lbce01 = para.get("lbce01").toString();

         Map map = null;
         for (int i = 0; i < ib; i++) {
             map = (Map) inBdj.get(i);
             map.put("bdj01", ls.get(i));
             map.put("lbce01", lbce01);
         }

         hosNewAdviceDao.insertCviBdj1(inBdj);
     }


 }

 /**
  * 插入历史记录
  *
  * @param para
  * @param tmpVAF
  */
 private void insertCviVbg1(Map para, List tmpVAF) throws Exception {
     int tv = tmpVAF.size();
     List<Long> ls = getNewId.nextId(tv);
     Map map = null;
     for (int i = 0; i < tv; i++) {
         map = (Map) tmpVAF.get(i);
         map.put("vbgid", ls.get(i));
     }

     para.put("tmpVAF", tmpVAF);

     hosNewAdviceDao.insertCviVbg1(para);
 }


 private void insertCviVao1(Map para) throws Exception {
     hosNewAdviceDao.insertCviVao1(para);
 }

 private void insertCviVaf1(Map para) throws Exception {
     hosNewAdviceDao.insertCviVaf1(para);
 }

 /**
  * 插入病人申请单信息
  *
  * @param tmpVBQ
  * @param para
  * @throws Exception
  */
 private void insertCviVbq1(List tmpVBQ, Map para) throws Exception {
     int tv = tmpVBQ.size();
     if (tv == 0) {
         return;
     }

     String ldate = para.get("ldate").toString();
     String ldatec = ldate.substring(0, 4);

     Map map;
     Long vbqid;
     for (int i = 0; i < tv; i++) {
         map = (Map) tmpVBQ.get(i);
         vbqid = Long.valueOf(map.get("vbqid").toString());
         map.put("vbq15", "JC" + ldatec + String.format("%010d", vbqid));
     }

     hosNewAdviceDao.insertCviVbq1(tmpVBQ);

 }

 private void insertCviCBM1(Map inMap) throws Exception {
     hosNewAdviceDao.insertCviCBM1(inMap);
 }

 /**
  * 更新病人手术人员信息，获取病人医嘱附件
  *
  * @param tmpVAX
  * @param tmpVAF
  * @return
  */
 private List getCviTmpVBF(List tmpVAX, List tmpVAF) {
     //更新病人手术人员的医嘱内容ID
     int tvx = tmpVAX.size();
     if (tvx == 0) {
         return tmpVAX;
     }

     int tvf = tmpVAF.size();
     Map mapx;
     Map mapf;
     String cbmid;
     String vaf59;
     String rownr;
     String cbmidr;
     String vaf59r;
     String rownrr;
     for (int i = 0; i < tvx; i++) {
         mapx = (Map) tmpVAX.get(i);
         cbmid = MapUtils.getString(mapx, "cbmid");
         vaf59 = MapUtils.getString(mapx, "vaf59");
         rownr = MapUtils.getString(mapx, "rownr");
         for (int j = 0; j < tvf; j++) {
             mapf = (Map) tmpVAF.get(i);
             cbmidr = MapUtils.getString(mapf, "cbmid");
             vaf59r = MapUtils.getString(mapf, "vaf59");
             rownrr = MapUtils.getString(mapf, "rownr");
             if (cbmid.equals(cbmidr) && vaf59.equals(vaf59r) && rownr.equals(rownrr)) {
                 mapx.put("vaf01", mapf.get("vaf01"));
             }

         }
     }

     // 更新病人手术人员信息ID
     List<Long> ls = getNewId.nextId(tvx);
     for (int i = 0; i < tvx; i++) {
         mapx = (Map) tmpVAX.get(i);
         mapx.put("vax01", ls.get(i));
     }

     //获取病人医嘱附件信息
     List tmpVBF = new ArrayList();
     Map map = null;

     String ace01 = null;

     for (int i = 0; i < tvx; i++) {
         mapx = (Map) tmpVAX.get(i);
         ace01 = MapUtils.getString(mapx, "ace01");
         if ( "--".equals(ace01)) {
             map = new HashMap<>();
             map.put("vaf01", mapx.get("vaf01"));
             map.put("bce02", mapx.get("bce02"));
             map.put("bce03", mapx.get("bce03"));
             tmpVBF.add(map);
         }
     }

     int tvbf = tmpVBF.size();
     if (tvbf == 0) {
         return tmpVBF;
     }
     //更新病人医嘱附件ID
     ls = getNewId.nextId(tvbf);
     for (int i = 0; i < tvbf; i++) {
         map = (Map) tmpVBF.get(i);
         map.put("vbf01", ls.get(i));
     }
     return tmpVBF;
 }

 /**
  * 更新病人诊断ID
  *
  * @param tmpVAO
  * @param tmpCBM
  * @return
  */
 private List getCviUpTmpVAO(List tmpVAO, List tmpCBM) {
     int tv = tmpVAO.size();
     if (tv == 0) {
         return tmpVAO;
     }
     // 更新病人诊疗申请单ID
     List<Long> ls = getNewId.nextId(tv);
     Map map = null;
     for (int i = 0; i < tv; i++) {
         map = (Map) tmpVAO.get(i);
         map.put("vao01", ls.get(i));
     }

     //更新诊断信息中的医嘱单信息
     String cbmid = null;
     String cbmidtc = null;
     Map maptc = null;
     int tc = tmpCBM.size();
     for (int i = 0; i < tv; i++) {
         map = (Map) tmpVAO.get(i);
         cbmid = MapUtils.getString(map, "cbmid");
         for (int j = 0; j < tc; j++) {
             maptc = (Map) tmpCBM.get(j);
             cbmidtc = MapUtils.getString(maptc, "cbmid");
             if (cbmid.equals(cbmidtc)) {
                 map.put("cbm01", maptc.get("cbm01"));
             }
         }
     }


     return tmpVAO;
 }

 /**
  * 更新病人诊疗申请单ID
  *
  * @param tmpVBQ
  * @param tmpCBM
  */
 private List getUpdateTmpVBQ(List tmpVBQ, List tmpCBM) {
     int tv = tmpVBQ.size();
     if (tv == 0) {
         return tmpVBQ;
     }
     // 更新病人诊疗申请单ID
     List<Long> ls = getNewId.nextId(tv);
     Map map = null;
     for (int i = 0; i < tv; i++) {
         map = (Map) tmpVBQ.get(i);
         map.put("vbqid", ls.get(i));
     }

     //删除申请单信息中没有对应的医嘱单id信息
     List dls = new ArrayList();
     int tc = tmpCBM.size();
     String vbq01 = null;
     String cbmid = null;
     Map maptc = null;
     for (int i = 0; i < tv; i++) {
         map = (Map) tmpVBQ.get(i);
         vbq01 = MapUtils.getString(map, "vbq01");
         if (vbq01 != null) {
             for (int j = 0; j < tc; j++) {
                 maptc = (Map) tmpCBM.get(j);
                 cbmid = MapUtils.getString(maptc, "cbmid");
                 if (vbq01.equals(cbmid)) {
                     dls.add(map);
                 }
             }
         }
     }

     //更新医嘱单信息的
     tv = dls.size();

     for (int i = 0; i < tc; i++) {
         maptc = (Map) tmpCBM.get(i);
         cbmid = MapUtils.getString(maptc, "cbmid");
         for (int j = 0; j < tv; j++) {
             map = (Map) dls.get(j);
             vbq01 = MapUtils.getString(map, "vbq01");
             if (cbmid.equals(vbq01)) {
                 maptc.put("vbq01", map.get("vbqid"));
             }
         }

     }

     return dls;
 }

 /**
  * 组装医嘱内容信息
  *
  * @param tmpVAF
  * @param tmpCBM
  * @return
  * @throws Exception
  */
 private List getCviZz(List tmpVAF, List tmpCBM) throws Exception {

     // 获取vaf01
     int tv = tmpVAF.size();
     List<Long> ls = getNewId.nextId(tv);

     Map map = null;
     String vaf11 = null;
     String vaf27 = null;
     for (int i = 0; i < tv; i++) {
         map = (Map) tmpVAF.get(i);
         map.put("vaf01", ls.get(i));
         vaf11 = MapUtils.getString(map, "vaf11");
         vaf27 = MapUtils.getString(map, "vaf27");
         if ("1".equals(vaf11)) {
             map.put("vaf35", 0);
         }
         if (vaf27 == null || "0".equals(vaf27)) {
             map.put("vaf27", 1);
         }
     }

     // 获取医嘱内容的医嘱单ID
     int tc = tmpCBM.size();

     String cbmid = null;
     String cbmidtc = null;
     Map maptc = null;
     for (int i = 0; i < tv; i++) {
         map = (Map) tmpVAF.get(i);
         cbmid = MapUtils.getString(map, "cbmid");
         for (int j = 0; j < tc; j++) {
             maptc = (Map) tmpCBM.get(j);
             cbmidtc = MapUtils.getString(maptc, "cbmid");
             if (cbmid.equals(cbmidtc)) {
                 map.put("cbm01", maptc.get("cbm01"));
             }
         }
     }

     map = new HashMap<>();
     map.put("tmpVAF", tmpVAF);
     // 组装医嘱内容
     tmpVAF = hosNewAdviceDao.getCviZz(map);

     return tmpVAF;
 }

 /**
  * @param tmpCBM
  * @return
  */
 private String getCviRcbm(List tmpCBM) {
     int tc = tmpCBM.size();
     List<Long> ls = getNewId.nextId(tc);

     StringBuffer sbs = new StringBuffer();

     Map map = null;
     Long l = null;
     for (int i = 0; i < tc; i++) {
         map = (Map) tmpCBM.get(i);
         l = ls.get(i);
         map.put("cbm01", l);
         if (i != 0) {
             sbs.append("," + l);
         } else {
             sbs.append(l);
         }
     }
     return sbs.toString();
 }

 /**
  * 修改医嘱单 中的病人姓名
  *
  * @param tmpCBM
  * @return
  * @throws Exception
  */
 private List getCviBrxmTmpCBM(List tmpCBM) throws Exception {
     Map map = new HashMap<>();
     map.put("tmpCBM", tmpCBM);
     // 根据病人ID获取病人姓名
     List ls = hosNewAdviceDao.getCviBrxmTmpCBM(map);

     int l = ls.size();
     int tc = tmpCBM.size();

     Map lsm = null;
     Map tcm = null;
     String vaa01 = null;
     String vaa01l = null;
     for (int i = 0; i < tc; i++) {
         tcm = (Map) tmpCBM.get(i);
         vaa01 = MapUtils.getString(tcm, "vaa01");
         for (int j = 0; j < l; j++) {
             lsm = (Map) ls.get(i);
             vaa01l = MapUtils.getString(lsm, "vaa01");
             if (vaa01.equals(vaa01l)) {
                 map.put("vaa05", lsm.get("vaa05"));
             }
         }
     }

     return tmpCBM;
 }

 private List getCviUpGyks(List tmpVAF) {
     int l = tmpVAF.size();
     Map map = null;
     Map rMap = null;
     String bda01 = null;
     String vaf32 = null;
     String bck01b = null;
     String bck01d = null;
     String cbmid = null;
     String vaf59 = null;
     String rownr = null;
     String rcbmid = null;
     String rvaf59 = null;
     for (int i = 0; i < l; i++) {
         map = (Map) tmpVAF.get(i);
         bda01 = MapUtils.getString(map, "bda01");
         vaf32 = MapUtils.getString(map, "vaf32");
         bck01b = MapUtils.getString(map, "bck01b");
         bck01d = MapUtils.getString(map, "bck01d");
         if ((bda01 != null && vaf32 != null && bck01b != null) && (("8".equals(bda01) || "T".equals(bda01))
                 && "1".equals(vaf32) && (Integer.valueOf(bck01b) > 0) && (bck01d == null || "0".equals(bck01d)))) {
             cbmid = MapUtils.getString(map, "cbmid");
             vaf59 = MapUtils.getString(map, "vaf59");
             for (int j = 0; j < l; j++) {
                 rMap = (Map) tmpVAF.get(j);
                 rcbmid = MapUtils.getString(rMap, "cbmid");
                 rvaf59 = MapUtils.getString(rMap, "vaf59");
                 rownr = MapUtils.getString(rMap, "rownr");
                 if (cbmid.equals(rcbmid) && vaf59.equals(rvaf59) && "1".equals(rownr)) {
                     map.put("bck01d", rMap.get("bck01b"));
                 }
             }
         }

     }

     for (int i = 0; i < l; i++) {
         map = (Map) tmpVAF.get(i);
         bda01 = MapUtils.getString(map, "bda01");
         bck01b = MapUtils.getString(map, "bck01b");
         bck01d = MapUtils.getString(map, "bck01d");
         if ((bda01 != null && bck01b != null) && (("1".equals(bda01) || "2".equals(bda01) || "3".equals(bda01))
                 && (Integer.valueOf(bck01b) > 0) && (bck01d == null || "0".equals(bck01d)))) {
             cbmid = MapUtils.getString(map, "cbmid");
             vaf59 = MapUtils.getString(map, "vaf59");
             for (int j = 0; j < l; j++) {
                 rMap = (Map) tmpVAF.get(j);
                 rcbmid = MapUtils.getString(rMap, "cbmid");
                 rvaf59 = MapUtils.getString(rMap, "vaf59");
                 rownr = MapUtils.getString(rMap, "rownr");
                 if (cbmid.equals(rcbmid) && vaf59.equals(rvaf59) && "0".equals(rownr)) {
                     map.put("bck01d", rMap.get("bck01b"));
                 }
             }
         }

     }

     return tmpVAF;
 }

 /**
  * 获取参数
  *
  * @return
  * @throws Exception
  */
 private Map getCviPara() throws Exception {
     Map map = hosNewAdviceDao.getCviPara();
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
     String ldate = sdf.format(new Date());
     map.put("ldate", ldate);
     map.put("pvalue", "0");
     map.put("fchange", 0);
     map.put("para203", "0");

     return map;
 }

 private ModelVo getCviNoyypc(List tmpVAF) {
     int l = tmpVAF.size();
     Map map = null;
     String bda01 = null;
     String vaf26 = null;
     for (int i = 0; i < l; i++) {
         map = (Map) tmpVAF.get(i);
         bda01 = MapUtils.getString(map, "bda01");
         vaf26 = MapUtils.getString(map, "vaf26");
         if (bda01 != null && (("1".equals(bda01) || "2".equals(bda01)) && (vaf26 == null || "".equals(vaf26)))) {
             return new ModelVo(StaticKeys.OH_FALSE, map.get("vaf22") + " 没有用药频次，请修改.");
         }
     }
     return null;
 }

 /**
  * 获取病人手术人员信息
  *
  * @param job
  * @return
  */
 private List getCviTmpVAX(JSONObject job) {
     if (!job.containsKey("vax1")) {
         return new ArrayList();
     }
     List ls = JsonUtil.parserToList(job.getJSONArray("vax1"));
     int l = ls.size();
     Map map = null;
     for (int i = 0; i < l; i++) {
         map = (Map) ls.get(i);
         map.put("vaf01", 0);
         map.put("vax01", 0);
         map.put("vat01", 0);
     }
     return ls;
 }

 /**
  * 获取病人诊疗申请单
  *
  * @param job
  * @return
  */
 private List getCviTmpVBQ(JSONObject job) {
     if (!job.containsKey("vbq1")) {
         return new ArrayList();
     }
     List ls = JsonUtil.parserToList(job.getJSONArray("vbq1"));
     int l = ls.size();
     Map map = null;
     for (int i = 0; i < l; i++) {
         map = (Map) ls.get(i);
         map.put("vbqid", 0);
     }
     return ls;
 }

 private List getCviTmpVAO(JSONObject job) {
     if (!job.containsKey("vao1")) {
         return new ArrayList();
     }
     List ls = JsonUtil.parserToList(job.getJSONArray("vao1"));
     int l = ls.size();
     Map map = null;
     for (int i = 0; i < l; i++) {
         map = (Map) ls.get(i);
         map.put("vao01", 0);
         map.put("cbm01", 0);
     }
     return ls;
 }

 /**
  * 提交文档错误，医嘱开始时间不能省略. 这个可在客户端完成
  *
  * @param tmpVAF
  * @return
  */
 private ModelVo getCviNoKzsj(List tmpVAF) {
     int l = tmpVAF.size();
     Map map = null;
     for (int i = 0; i < l; i++) {
         map = (Map) tmpVAF.get(i);
         String vaf36 = MapUtils.getString(map, "vaf36");
         if (vaf36 == null) {
             return new ModelVo(StaticKeys.OH_FALSE, "提交文档错误，医嘱开始时间不能省略.");
         }
     }
     return null;
 }

 /**
  * 获取医嘱内容信息
  *
  * @param job
  * @return
  */
 private List getCviTmpVAF(JSONObject job, List tmpCBM) {

     int ll = tmpCBM.size();
     Set set = new HashSet<>();
     String cbmid = null;
     Map map = null;
     for (int i = 0; i < ll; i++) {
         map = (Map) tmpCBM.get(i);
         cbmid = MapUtils.getString(map, "cbmid");
         if (cbmid != null) {
             set.add(cbmid);
         }
     }

     List rs = new ArrayList<>();

     List ls = JsonUtil.parserToList(job.getJSONArray("vaf1"));
     ll = ls.size();
     String lsign = null;

     for (int i = 0; i < ll; i++) {
         map = (Map) ls.get(i);
         lsign = MapUtils.getString(map, "lsign");
         if (lsign != null && "0".equals(lsign)) {
             cbmid = MapUtils.getString(map, "cbmid");
             if (set.contains(cbmid)) {
                 map.put("vaf01", 0);
                 map.put("vaf01a", 0);
                 map.put("vaf75", 0);
                 map.put("vaf51", 0);
                 map.put("cbm01", 0);
                 map.put("bag46", 0d);
                 map.put("audit_type", 0);
                 rs.add(map);
             }
         }
     }

     return rs;
 }

 /**
  * 获取医嘱单信息
  *
  * @param job
  * @return
  */
 private List getCviTmpCBM(JSONObject job, Integer lbce01, String lbce03) {
     List ls = JsonUtil.parserToList(job.getJSONArray("cbm1"));
     int l = ls.size();
     Map map = null;
     for (int i = 0; i < l; i++) {
         map = (Map) ls.get(i);
         map.put("bce01", lbce01);
         map.put("bce03", lbce03);
         map.put("cbm01", 0);
         map.put("vbq01", 0);
     }

     return ls;
 }

 /**
  * 根据医嘱单id删除医嘱
  * Clinic_Advice_CBM01_Del
  *
  * @param map
  * @return
  */
 public ModelVo delclinicAdviceCBM01(Map map) 
 {
 	/*
 	 *新增医嘱修改状态信息
		 *根据医嘱ID删除：1、诊断结果；2、诊疗信息；3、医嘱单；4、医嘱记录
		 */
		
		ModelVo mv = new ModelVo();
		try
		{
		    List<Long> ls = getNewId.nextId((int)(Math.random() * 100));
			map.put("VBG01", ls.get(0).intValue());
//		    map.put("txt", "select wjProc()");
			hosNewAdviceDao.addVBG1(map);
			hosNewAdviceDao.delVAO1ByCBM01(map);
			hosNewAdviceDao.delVBQ1ByCBM01(map);
			
			hosNewAdviceDao.delCBM1ByCBM01(map);
			hosNewAdviceDao.delVAF1ByCBM01(map);
			
			mv.setResultMsg(StaticKeys.OH_SUCCESS);
			return mv;
		}
		catch (Exception e)
		{
			mv.setResultMsg(StaticKeys.OH_FALSE);
			return mv;
		}
		
 }
	
	private void insertCAM1(Map map)throws Exception{
		map.put("lCAM01", getNewId.nextId());
		hosNewAdviceDao.insertCAM1(map);
	}
	private void insertCAH1(Map map)throws Exception{
		map.put("lCAH01", getNewId.nextId());
		hosNewAdviceDao.insertCAM1(map);
	}
	public ModelVo execClinicCAMInsert(Map map)throws Exception{
		ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
		//添加疾病目录
		insertCAM1(map);
		//添加疾病别名
		insertCAH1(map);
		
		return mo;
	}
}
